package com.sr.examination.other;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

import com.sr.examination.R;
import com.sr.examination.net.Net;
import com.tl.commonlibrary.network.ErrorResponse;
import com.tl.commonlibrary.network.RequestListener;
import com.tl.commonlibrary.network.bean.base.BaseBean;
import com.tl.commonlibrary.tool.UIUtils;
import com.tl.commonlibrary.ui.BaseFragmentActivity;
import com.tl.commonlibrary.ui.widget.CustomAlertDialog;
import com.tl.commonlibrary.ui.widget.WarnDialog;

import retrofit2.Call;

/**
 * Created by JongSung on 18/2/25.
 */

public class FeedBackActivity extends BaseFragmentActivity {
    public static void start(Context context) {
        Intent starter = new Intent(context, FeedBackActivity.class);
        context.startActivity(starter);
    }

    private CustomAlertDialog giveUpDialog;
    private WarnDialog exceedDialog;
    private EditText contentEText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_feedback);
        setTitle(getString(R.string.feed_back));
        findViewById(R.id.submitBtn).setOnClickListener(this);
        contentEText = findViewById(R.id.feedbackEText);
    }

    private void postFeedBack(String content) {
        showProgressDialog();
        Net.feedback(content, new RequestListener<BaseBean>() {
            @Override
            public void onSuccess(Call<BaseBean> call, BaseBean response) {
                if (response.isSuccessful()) {
                    UIUtils.showToast(getString(R.string.hint_feedback_success));
                    finish();
                }
                dismissAll();
            }

            @Override
            public void onFailed(Call<BaseBean> call, ErrorResponse errorResponse) {
                dismissAll();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submitBtn:
                String content = contentEText.getText().toString().trim();
                if (TextUtils.isEmpty(content)) {
                    UIUtils.showToast(getString(R.string.hint_feed_back_empty));
                    return;
                }
                if (content.length() > 100) {
                    createExceedDialog();
                    exceedDialog.show();
                    return;
                }
                postFeedBack(content);
                break;
        }
    }

    @Override
    public void back() {
        if (!TextUtils.isEmpty(contentEText.getText().toString().trim())) {
            createGiveUpDialog();
            giveUpDialog.show();
        } else {
            super.back();
        }
    }

    private void createGiveUpDialog() {
        if (giveUpDialog == null) {
            giveUpDialog = new CustomAlertDialog(context);
            giveUpDialog.setContent(getString(R.string.give_up_feedback));
            giveUpDialog.setListener(new CustomAlertDialog.OnAlertClickListener() {
                @Override
                public void onRightClicked(View view) {
                    finish();
                }

                @Override
                public void onLeftClicked(View view) {

                }
            });
        }
    }

    private void createExceedDialog() {
        if (exceedDialog == null) {
            exceedDialog = new WarnDialog(context);
            exceedDialog.setContent(getString(R.string.feed_back_exceed));
//            exceedDialog.setListener(new WarnDialog.OnWarnClickListener() {
//
//                @Override
//                public void onWarnClicked(View view) {
//
//                }
//            });
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if (!TextUtils.isEmpty(contentEText.getText().toString().trim())) {
                createGiveUpDialog();
                giveUpDialog.show();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onFragmentTrigger(int tag, Bundle bundle) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (giveUpDialog != null) {
            giveUpDialog.dismiss();
            giveUpDialog = null;
        }
        if (exceedDialog != null) {
            exceedDialog.dismiss();
            exceedDialog = null;
        }
    }
}
